package demo.business;

import jk.fox.common.payload.request.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.UUID;

/**
 * @Auther cuichao
 * @Description: TestInvoke
 * @Date: create in 2020/3/30 20:53
 */
public class TestInvoke implements InvocationHandler {

    /*private String ip = "127.0.0.1";
    private int port = 7779;
    private NettyClientTransporter jk.fox.node;
    private ChannelFuture channelFuture;*/

    //private RpcClient rpcClient = new RpcClient();

    public TestInvoke(){
       /* jk.fox.node = new NettyClientTransporter();

        if(channelFuture == null){
            try {
                channelFuture = jk.fox.node.create(ip, port);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/


    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        RpcRequest request = new RpcRequest(
                UUID.randomUUID().toString().replaceAll("-",""),
                method.getName(),
                args
                );

        //RpcResponse send = (RpcResponse) jk.fox.node.send(channelFuture, request);
        //推断方法返回值
        //Object returnValue = determineReturnValue(method, send.getResponse());
        //Object response = send.getResponse();


        //Object response = rpcClient.execute(method.getName(), args);


/*        System.out.println(response);
        return response;*/
        return null;
    }

    /**
     * 推断返回值
     * @param method
     * @param response
     * @return
     */
    private Object determineReturnValue(Method method,Object response) throws Exception {
        String name = method.getName();
      /*  ObjectDescriptor objectDescriptor = ObjectDescriptor.get(name);
        if(objectDescriptor == null ){
            synchronized (this){
                if(ObjectDescriptor.get(name) == null){
                    objectDescriptor = new ObjectDescriptor();

                }
            }
        }*/


        method.getGenericReturnType();
        Class<?> returnType = method.getReturnType();
        Type genericType = method.getGenericReturnType();
        //判断是否带有泛型
        Type[] types = null;
        if(genericType instanceof ParameterizedType){
            types = ((ParameterizedType) genericType).getActualTypeArguments();// 强制转型为带参数的泛型类型，
        }
        //判断是否为集合类型
        if(Collection.class.isAssignableFrom(returnType)){

            /*if(types != null &&  types.length > 0){
                Type type = types[0];
                Class cls = Class.forName(type.getTypeName());
                if(response instanceof String) {
                    List list = JSON.parseArray(response.toString(), cls);
                    return list;
                }else if(response instanceof JSONArray){
                    JSONArray array = (JSONArray)response;
                    List list = array.toJavaList(cls);
                    return list;
                }else {
                    throw new  InvalidObjectException("返回类型不是list类型");
                }
            }*/
        }
        return null;
    }



    private boolean isConllection(){
        return false;
    }

}
